<template>
  <div class="app-container">
    <!-- 查询条件组件 -->
    <SelectForm :SelectForm="SelectList" @selectSubmit="selectSubmit" />
    <el-card>
      <el-button type="primary" @click="handleAdd" v-hasPermi="['system:post:add']">新增</el-button>
      <!-- <el-button type="success" :disabled="single" @click="handleUpdate" v-hasPermi="['system:post:edit']"
        >修改</el-button
      > -->
      <el-button type="danger" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:post:remove']"
        >删除</el-button
      >
      <el-button type="warning" @click="handleExport" v-hasPermi="['system:post:export']">导出</el-button>

      <el-table
        class="tableClass"
        border
        v-loading="loading"
        :data="postList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" fixed="left" />
        <el-table-column label="工种编号" align="center" prop="postId" />
        <el-table-column label="工种编码" align="center" prop="postCode" />
        <el-table-column label="工种名称" align="center" prop="postName" />
        <!-- <el-table-column label="工种排序" align="center" prop="postSort" /> -->
        <el-table-column label="状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="createBy" width="180">
          <template #default="scope">
            <span>{{ scope.row.createBy }}</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-link @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-link>
            <el-link @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-link>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改工种对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工种名称" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入工种名称" />
        </el-form-item>
        <el-form-item label="工种编码" prop="postCode">
          <el-input v-model="form.postCode" placeholder="请输入编码名称" />
        </el-form-item>
        <!-- <el-form-item label="工种顺序" prop="postSort">
          <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
        </el-form-item> -->
        <el-form-item label="工种状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{
              dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import { listPost, addPost, delPost, getPost, updatePost } from '@/api/system/post'

const { proxy } = getCurrentInstance()
const { sys_normal_disable } = proxy.useDict('sys_normal_disable')

const postList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')

/**
 * 查询枚举
 */
const SelectList = ref([
  { label: '', type: '日期范围' },
  { label: '工种编码', model: 'postCode', type: '输入框' },
  { label: '工种名称', model: 'postName', type: '输入框' },
  { label: '状态', model: 'status', type: '下拉单选', opt: sys_normal_disable }
])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    postCode: undefined,
    postName: undefined,
    status: undefined
  },
  rules: {
    postName: [{ required: true, message: '工种名称不能为空', trigger: 'blur' }],
    postCode: [{ required: true, message: '工种编码不能为空', trigger: 'blur' }]
    // postSort: [{ required: true, message: '工种顺序不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询工种列表 */
function getList() {
  loading.value = true
  listPost(queryParams.value).then(response => {
    postList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    // postSort: 0,
    status: '0',
    remark: undefined
  }
  proxy.resetForm('postRef')
}
/** 搜索按钮操作 */
function selectSubmit(val) {
  queryParams.value = val
  queryParams.value.pageNum = 1
  queryParams.value.pageSize = 10
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef')
  handleQuery()
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.postId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加工种'
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const postId = row.postId || ids.value
  getPost(postId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改工种'
  })
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs['postRef'].validate(valid => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPost(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}
/** 删除按钮操作 */
function handleDelete(row) {
  const postIds = row.postId || ids.value
  proxy.$modal
    .confirm('是否确认删除工种编号为"' + postIds + '"的数据项？')
    .then(function () {
      return delPost(postIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'system/post/export',
    {
      ...queryParams.value
    },
    `post_${new Date().getTime()}.xlsx`
  )
}

getList()
</script>
